package com.qs.eggs.view.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.DefaultSliderView;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.pay.PayActivity;
import com.qs.eggs.view.recharge.activity.RechargeActivity;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class PayMethodActivity extends Activity implements View.OnClickListener{
    private RelativeLayout chong;
    private Toolbar back;
    private RadioButton rb0,rb1,rb2,rb3;
    private int ids;
    private Button topay;
    private TextView money,rmb;
    private String balance,orderMoney,orderno;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_method);
        orderMoney=getIntent().getStringExtra("ordermoney");
        orderno=getIntent().getStringExtra("orderno");
        initView();
        getMoney();
    }
    private void initView(){
        chong=(RelativeLayout)findViewById(R.id.rl_chongzhi);
         back=(Toolbar)findViewById(R.id.toolbar);
        rb0=(RadioButton)findViewById(R.id.rb0);
        rb1=(RadioButton)findViewById(R.id.rb1);
        rb2=(RadioButton)findViewById(R.id.rb2);
        rb3=(RadioButton)findViewById(R.id.rb3);
        rmb=(TextView)findViewById(R.id.rmb);
        topay=(Button)findViewById(R.id.ok);
        money=(TextView)findViewById(R.id.money);
        chong.setOnClickListener(this);
        back.setOnClickListener(this);
        topay.setOnClickListener(this);
        rmb.setText("¥"+orderMoney+"");
        //支付宝支付
        rb0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    rb1.setChecked(false);
                    rb2.setChecked(false);
                    rb3.setChecked(false);
                    ids=0;
                }
            }
        });
        //微信支付
        rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    rb0.setChecked(false);
                    rb2.setChecked(false);
                    rb3.setChecked(false);
                    ids=1;
                }
            }
        });
        //账户余额
        rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    rb0.setChecked(false);
                    rb1.setChecked(false);
                    rb3.setChecked(false);
                    ids=2;
                }
            }
        });
        //货到付款
        rb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    rb0.setChecked(false);
                    rb1.setChecked(false);
                    rb2.setChecked(false);
                    ids=3;
                }
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_chongzhi:
                Intent intent=new Intent(PayMethodActivity.this,RechargeActivity.class);
                startActivity(intent);
             break;
            case R.id.toolbar:
                PayMethodActivity.this.finish();
                break;
            case R.id.ok:
                //支付宝支付
               if (ids==0){
//                   Intent intent1 = new Intent(PayMethodActivity.this, PayActivity.class);
//                   intent1.putExtra("orderNo", orderno);
//                   intent1.putExtra("orderMoney", orderMoney);
//                   startActivity(intent1);
               }
               //微信支付
                if (ids==1){

                }
                //账户余额支付
                if (ids==2){
                    if (Double.parseDouble(balance)<Double.parseDouble(orderMoney)){
                        Toast.makeText(PayMethodActivity.this,"账户余额不足",Toast.LENGTH_LONG).show();
                    }else{
                        PayA();
                    }
                }
                //货到付款
                if (ids==3){
                    PayB();
                }
                break;
        }
      }
      //获得账户余额
      private void getMoney(){
          OkGo.post(Constants.URL_BASE+"mallLogin?action=searchUserInfo")
                  .execute(new StringCallback() {
                      @Override
                      public void onSuccess(String s, Call call, Response response) {
                          System.out.print("个人信息=="+s);
                          try {
                              JSONArray jsonArray = new JSONArray(s);
                              for (int i = 0; i < jsonArray.length(); i++) {
                                  balance=jsonArray.getJSONObject(i).getString("balance");
                                  money.setText("(可用余额¥"+balance+")");
                              }

                          } catch (Exception e) {
                              e.printStackTrace();
                          }
                      }
                  });
      }
      private void PayA(){
          new AlertDialog.Builder(PayMethodActivity.this)
                  .setTitle("余额支付")
                  .setMessage("确定此订单使用余额支付吗")
                  .setPositiveButton("确定",
                          new DialogInterface.OnClickListener() {

                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  HashMap<String,String> map=new HashMap<String, String>();
                                  map.put("orderno",orderno);
                                  Gson gson=new Gson();
                                  String str=gson.toJson(map);
                                  OkGo.post(Constants.URL_BASE+"zhifubao?action=yueZhiFu")
                                          .params("data",str)
                                          .execute(new StringCallback() {
                                              @Override
                                              public void onSuccess(String s, Call call, Response response) {
                                                  System.out.print("余额支付=="+s);
                                                  if (s.equals("\"true\"")){
                                                      PayMethodActivity.this.finish();
                                                      Intent intent=new Intent(PayMethodActivity.this,OrderManagerActivity.class);
                                                      startActivity(intent);
                                                  }else{
                                                      Toast.makeText(PayMethodActivity.this,"支付失败",Toast.LENGTH_SHORT).show();
                                                  }
                                              }
                                          });
                                  dialog.dismiss();
                              }
                          })
                    .setNegativeButton("取消",new DialogInterface.OnClickListener() {

                      @Override
                      public void onClick(DialogInterface dialog,int which) {
                      }
                  }).setCancelable(false).show();


      }
      private void PayB(){
          new AlertDialog.Builder(PayMethodActivity.this)
                  .setTitle("货到付款")
                  .setMessage("确定支付方式为货到付款吗")
                  .setPositiveButton("确定",
                          new DialogInterface.OnClickListener() {

                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  HashMap<String,String> map=new HashMap<String, String>();
                                  map.put("orderno",orderno);
                                  Gson gson=new Gson();
                                  String str=gson.toJson(map);
                                  OkGo.post(Constants.URL_BASE+"zhifubao?action=xianXiaZhiFu")
                                          .params("data",str)
                                          .execute(new StringCallback() {
                                              @Override
                                              public void onSuccess(String s, Call call, Response response) {
                                                  System.out.print("货到付款=="+s);
                                                  if (s.equals("\"true\"")){
                                                      PayMethodActivity.this.finish();
                                                      Intent intent=new Intent(PayMethodActivity.this,OrderManagerActivity.class);
                                                      startActivity(intent);
                                                  }
                                              }
                                          });
                                  dialog.dismiss();
                              }
                          })
                  .setNegativeButton("取消",new DialogInterface.OnClickListener() {

                      @Override
                      public void onClick(DialogInterface dialog,int which) {
                      }
                  }).setCancelable(false).show();

      }
}
